//
//
//  NSDate+IDPExtension.m
//  IDP
//
//  Created by douj on 13-3-6.
//  Copyright (c) 2012年 baidu. All rights reserved.
//
#import "NSString+IDPExtension.h"
#import "NSDate+IDPExtension.h"

#define DATE_COMPONENTS (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfMonth |  NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal)
#define CURRENT_CALENDAR [NSCalendar currentCalendar]

@implementation NSDate (IDPExtension)

- (NSString *)stringWithDateFormat:(NSString *)format
{
	NSTimeInterval time = [self timeIntervalSince1970];
	NSUInteger timeUint = (NSUInteger)time;
	
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
	NSString * result = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeUint]];
	return result;
}

+ (NSTimeInterval)timeIntervalSince1970
{
    NSDate *date = [[NSDate alloc] init];
    NSTimeInterval timeInterval = [date timeIntervalSince1970];
    return timeInterval;
}

#pragma mark Relative Dates

+ (NSDate *) dateCWithDaysFromNow: (NSUInteger) days
{
	NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_DAY * days;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}

+ (NSDate *) dateWithDaysCBeforeNow: (NSUInteger) days
{
	NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] - D_DAY * days;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}

+ (NSDate *) dateTomorrow
{
	return [NSDate dateCWithDaysFromNow:1];
}

+ (NSDate *) dateYesterday
{
	return [NSDate dateWithDaysCBeforeNow:1];
}

+ (NSDate *) dateWithHoursFromNow: (NSUInteger) dHours
{
	NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_HOUR * dHours;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}

+ (NSDate *) dateWithHoursBeforeNow: (NSUInteger) dHours
{
	NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] - D_HOUR * dHours;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}

+ (NSDate *) dateWithMinutesFromNow: (NSUInteger) dMinutes
{
	NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}

+ (NSDate *) dateWithMinutesBeforeNow: (NSUInteger) dMinutes
{
	NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] - D_MINUTE * dMinutes;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}

#pragma mark Comparing Dates

- (BOOL) isEqualToDateIgnoringTime: (NSDate *) aDate
{
	NSDateComponents *components1 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	NSDateComponents *components2 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:aDate];
	return (([components1 year] == [components2 year]) &&
			([components1 month] == [components2 month]) &&
			([components1 day] == [components2 day]));
}

- (BOOL) isToday
{
	return [self isEqualToDateIgnoringTime:[NSDate date]];
}

- (BOOL) isTomorrowCustom
{
	return [self isEqualToDateIgnoringTime:[NSDate dateTomorrow]];
}

- (BOOL) isYesterday
{
	return [self isEqualToDateIgnoringTime:[NSDate dateYesterday]];
}

// This hard codes the assumption that a week is 7 days
- (BOOL) isSameWeekAsDate: (NSDate *) aDate
{
	NSDateComponents *components1 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	NSDateComponents *components2 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:aDate];
	
	// Must be same week. 12/31 and 1/1 will both be week "1" if they are in the same week
	if ([components1 weekOfMonth] != [components2 weekOfMonth]) return NO;
	
	// Must have a time interval under 1 week. Thanks @aclark
	return (fabs([self timeIntervalSinceDate:aDate]) < D_WEEK);
}

- (BOOL) isThisWeek
{
	return [self isSameWeekAsDate:[NSDate date]];
}

- (BOOL) isNextWeek
{
	NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_WEEK;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return [self isSameYearAsDateCustom:newDate];
}

- (BOOL) isLastWeek
{
	NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] - D_WEEK;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return [self isSameYearAsDateCustom:newDate];
}

- (BOOL) isSameYearAsDateCustom: (NSDate *) aDate
{
	NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
	NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:aDate];
	return ([components1 year] == [components2 year]);
}

- (BOOL) isThisYear
{
	return [self isSameWeekAsDate:[NSDate date]];
}

- (BOOL) isNextYear
{
	NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
	NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:[NSDate date]];
	
	return ([components1 year] == ([components2 year] + 1));
}

- (BOOL) isLastYear
{
	NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
	NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:[NSDate date]];
	
	return ([components1 year] == ([components2 year] - 1));
}

- (BOOL) isEarlierThanDate: (NSDate *) aDate
{
	return ([self earlierDate:aDate] == self);
}

- (BOOL) isLaterThanDate: (NSDate *) aDate
{
	return ([self laterDate:aDate] == self);
}


#pragma mark Adjusting Dates

- (NSDate *) dateByAddingCDays: (NSUInteger) dDays
{
	NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_DAY * dDays;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}

- (NSDate *) dateBySubtractingDays: (NSUInteger) dDays
{
	return [self dateByAddingCDays: (dDays * -1)];
}

- (NSDate *) dateByAddingHoursCustom: (NSUInteger) dHours
{
	NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_HOUR * dHours;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}

- (NSDate *) dateBySubtractingHours: (NSUInteger) dHours
{
	return [self dateByAddingHoursCustom: (dHours * -1)];
}

- (NSDate *) dateByAddingCMinutes: (NSUInteger) dMinutes
{
	NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}

- (NSDate *) dateBySubtractingMinutes: (NSUInteger) dMinutes
{
	return [self dateByAddingCMinutes: (dMinutes * -1)];
}

- (NSDate *) dateAtStartOfDay
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	[components setHour:0];
	[components setMinute:0];
	[components setSecond:0];
	return [CURRENT_CALENDAR dateFromComponents:components];
}

- (NSDateComponents *) componentsWithOffsetFromDate: (NSDate *) aDate
{
	NSDateComponents *dTime = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:aDate toDate:self options:0];
	return dTime;
}

#pragma mark Retrieving Intervals

- (NSInteger) minutesAfterDate: (NSDate *) aDate
{
	NSTimeInterval ti = [self timeIntervalSinceDate:aDate];
	return (NSInteger) (ti / D_MINUTE);
}

- (NSInteger) minutesBeforeDate: (NSDate *) aDate
{
	NSTimeInterval ti = [aDate timeIntervalSinceDate:self];
	return (NSInteger) (ti / D_MINUTE);
}

- (NSInteger) hoursAfterDate: (NSDate *) aDate
{
	NSTimeInterval ti = [self timeIntervalSinceDate:aDate];
	return (NSInteger) (ti / D_HOUR);
}

- (NSInteger) hoursBeforeDate: (NSDate *) aDate
{
	NSTimeInterval ti = [aDate timeIntervalSinceDate:self];
	return (NSInteger) (ti / D_HOUR);
}

- (NSInteger) daysAfterDate: (NSDate *) aDate
{
	NSTimeInterval ti = [self timeIntervalSinceDate:aDate];
	return (NSInteger) (ti / D_DAY);
}

- (NSInteger) daysBeforeDate: (NSDate *) aDate
{
	NSTimeInterval ti = [aDate timeIntervalSinceDate:self];
	return (NSInteger) (ti / D_DAY);
}

#pragma mark 转换日期为string
+ (NSString *)convertDateIntervalToStringWith:(NSString *)aInterVal
{
  time_t    statusCreateAt_t;
  NSString* timestamp = nil;
  time_t now;
  time(&now);
  
	statusCreateAt_t = [aInterVal doubleValue];
	
	struct tm *nowtime;
	nowtime = localtime(&now);
	uint32_t thour = nowtime->tm_hour;
	
//	struct tm *ptime;
//	ptime = localtime(&statusCreateAt_t);
	
  int distance = (int)difftime( statusCreateAt_t,now);
  if (distance < 0) distance = 0;
  
if (distance < 60)
  {
     timestamp = [NSString stringWithFormat:@"剩%d秒",distance];
  }
  else if (distance < 60 * 60) /* 小于1小时 */
  {
    distance = distance / 60;
		if (distance == 0) {
			distance = 1;
		}
    timestamp = [NSString stringWithFormat:@"剩%d分钟", distance];
  }
  else if (distance < (60 * 60 * (thour))) /* 大于1小时，但是在今日 */
  {
      /*
		int mins=ptime->tm_min;
		if(mins<10){
			timestamp =[NSString stringWithFormat:@"%d:0%d",ptime->tm_hour ,ptime->tm_min];
		}
		else
    {
			timestamp =[NSString stringWithFormat:@"%d:%d",ptime->tm_hour ,ptime->tm_min];
		}
       */
      distance = distance / (60*60);
      timestamp =[NSString stringWithFormat:@"剩%d小时",distance];
  }
  else if (distance >= (60 * 60 * 24) /*&& distance < (60 * 60*24*31)*/)
  {
    distance = distance / (60*60*24);
		if (distance == 0) {
			distance = 1;
		}
    timestamp = [NSString stringWithFormat:@"剩%d天", distance];
  }
    /*
  else
  {

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    
		[dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:statusCreateAt_t];
		timestamp = [dateFormatter stringFromDate:date];
//		int year = [timestamp intValue];
//		
//		NSDate *date_now = [NSDate dateWithTimeIntervalSince1970:now];
//		NSString* timestamp_now = [dateFormatter stringFromDate:date_now];
//		int year_now = [timestamp_now intValue];
//		
//		if (year == year_now)
//    {
//			[dateFormatter setDateFormat:@"MM月dd日"];
//			NSDate *date = [NSDate dateWithTimeIntervalSince1970:statusCreateAt_t];
//			timestamp = [dateFormatter stringFromDate:date];
//		}
    
    [dateFormatter release];
	}
     */
	return timestamp;

}



#pragma mark 转换日期为string
+ (NSString *)convertDateIntervalToStringWith:(long long)max min:(long long) min
{
    time_t now;
    time(&now);
	struct tm *nowtime;
	nowtime = localtime(&now);
	uint32_t thour = nowtime->tm_hour;
	NSString* timestamp = nil;
    int distance = (int)difftime((double)max,(double)min);
    if (distance < 0) distance = 0;
    
    if (distance < 60)
    {
        timestamp = [NSString stringWithFormat:@"%d秒",distance];
    }
    else if (distance < 60 * 60) /* 小于1小时 */
    {
        distance = distance / 60;
		if (distance == 0) {
			distance = 1;
		}
        timestamp = [NSString stringWithFormat:@"%d分钟", distance];
    }
    else if (distance < (60 * 60 * (thour))) /* 大于1小时，但是在今日 */
    {
        /*
         int mins=ptime->tm_min;
         if(mins<10){
         timestamp =[NSString stringWithFormat:@"%d:0%d",ptime->tm_hour ,ptime->tm_min];
         }
         else
         {
         timestamp =[NSString stringWithFormat:@"%d:%d",ptime->tm_hour ,ptime->tm_min];
         }
         */
        distance = distance / (60*60);
        timestamp =[NSString stringWithFormat:@"%d小时",distance];
    }
    else if (distance >= (60 * 60 * 24) /*&& distance < (60 * 60*24*31)*/)
    {
        distance = distance / (60*60*24);
		if (distance == 0) {
			distance = 1;
		}
        timestamp = [NSString stringWithFormat:@"%d天", distance];
    }
    /*
     else
     {
     
     NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
     
     [dateFormatter setDateFormat:@"yyyy-MM-dd"];
     NSDate *date = [NSDate dateWithTimeIntervalSince1970:statusCreateAt_t];
     timestamp = [dateFormatter stringFromDate:date];
     //		int year = [timestamp intValue];
     //
     //		NSDate *date_now = [NSDate dateWithTimeIntervalSince1970:now];
     //		NSString* timestamp_now = [dateFormatter stringFromDate:date_now];
     //		int year_now = [timestamp_now intValue];
     //
     //		if (year == year_now)
     //    {
     //			[dateFormatter setDateFormat:@"MM月dd日"];
     //			NSDate *date = [NSDate dateWithTimeIntervalSince1970:statusCreateAt_t];
     //			timestamp = [dateFormatter stringFromDate:date];
     //		}
     
     [dateFormatter release];
     }
     */
	return timestamp;
    
}



+ (NSString *)convertDateFomatterToStringWith:(NSString *)aInterVal  formatter:(NSString *)daFormatter
{
    if (nil == daFormatter) {
        return @"";
    }
    long long time  = [aInterVal longLongValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [dateFormatter setDateFormat:daFormatter];
    NSDate *theday = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *day = [dateFormatter stringFromDate:theday];
    return day;
}

+ (NSString *)convertDateFomatterToStringWith:(NSString *)aInterVal {
    long long time  = [aInterVal longLongValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *theday = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *day = [dateFormatter stringFromDate:theday];
    return day;
}

+ (NSString *)convertDateHM:(NSString *)aInterVal {
    long long time  = [aInterVal longLongValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSDate *theday = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *day = [dateFormatter stringFromDate:theday];
    return day;
}

+ (NSString *)convertDateyHd:(NSString *)aInterVal {
    long long time  = [aInterVal longLongValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *theday = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *day = [dateFormatter stringFromDate:theday];
    return day;
}

+ (NSString *)convertDateYM:(NSString *)aInterVal {
    long long time  = [aInterVal longLongValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSDate *theday = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *day = [dateFormatter stringFromDate:theday];
    return day;
}


+ (NSDate *) stringToDate:(NSString *)stringFormat {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateTime = [dateFormatter dateFromString:stringFormat];
    return dateTime;
}

+ (NSString *)getTimeFomatterToStringWith:(NSString *)aInterVal {
    long long time  = [aInterVal longLongValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *theday = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *day = [dateFormatter stringFromDate:theday];
    return day;
}

+ (NSString *)getCurrentDateTwo
{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
    [dateformate setDateFormat:@"yyyy-MM-dd"];
    return [dateformate stringFromDate:localeDate];
}

+ (NSString *)getActivityTimeWithInterVal:(NSString *)aInterVal
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSString *todayStr = [formatter stringFromDate:[NSDate date]];
    long long interValValue = [aInterVal longLongValue];
    if (aInterVal.length>10) {
        interValValue = interValValue/1000;
    }
    NSDate *interfaceDate = [NSDate dateWithTimeIntervalSince1970:interValValue];
    NSString *interfaceStr = [formatter stringFromDate:interfaceDate];
    if ([todayStr isEqualToString:interfaceStr]) {
        return @"今天";
    }
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    return [formatter stringFromDate:interfaceDate];
}

+ (NSString *)getTimeWithTimeStamp:(NSString *)timeStamp
{
    //时间戳转时间的方法
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSDateFormatter* formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"MM月dd日"];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"HH:mm"];
    NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
    [formatter3 setDateFormat:@"MM月dd日 HH:mm"];
    NSDate *interfaceDate = [NSDate dateWithTimeIntervalSince1970:[timeStamp longLongValue]];
    NSString *interfaceDateStr = [formatter stringFromDate:interfaceDate];
    NSString *currentDateStr = [formatter stringFromDate:[NSDate date]];
    int yesterday = [currentDateStr intValue]-1;
    NSString *yesterdayStr = [NSString stringWithFormat:@"%d",yesterday];
    if ([interfaceDateStr isEqualToString:currentDateStr]) {
        return [formatter2 stringFromDate:interfaceDate];
    }
    else if ([interfaceDateStr isEqualToString:yesterdayStr]){
        return [NSString stringWithFormat:@"昨日 %@",[formatter2 stringFromDate:interfaceDate]];
    }
    else{
        return [formatter3 stringFromDate:interfaceDate];
    }
    return [formatter1 stringFromDate:interfaceDate];
}

#pragma mark Decomposing Dates

- (NSInteger) nearestHour
{
	NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_MINUTE * 30;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	NSDateComponents *components = [CURRENT_CALENDAR components:NSCalendarUnitHour fromDate:newDate];
	return [components hour];
}

- (NSInteger) hour
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components hour];
}

- (NSInteger) minute
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components minute];
}

- (NSInteger) seconds
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components second];
}

- (NSInteger) day
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components day];
}

- (NSInteger) month
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components month];
}

- (NSInteger) week
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components weekOfMonth];
}

- (NSInteger) weekday
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components weekday];
}

- (NSInteger) nthWeekday // e.g. 2nd Tuesday of the month is 2
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components weekdayOrdinal];
}
- (NSInteger) year
{
	NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
	return [components year];
}

+ (NSString *)getNextMouth:(NSString *)year_mouth {
    int year = [[year_mouth substringWithRange:NSMakeRange(0, 4)] intValue];
    int month = [[year_mouth substringFromIndex:5] intValue];
    month += 1;
    if (month >= 12) {
        year += 1;
        month = 1;
    }
    return [NSString stringWithFormat:@"%d-%@",year,[NSString format:month]];
}


+ (NSString *)getLastMouth:(NSString *)year_mouth {
    int year = [[year_mouth substringWithRange:NSMakeRange(0, 4)] intValue];
    int month = [[year_mouth substringFromIndex:5] intValue];
    month -= 1;
    if (month <= 0) {
        year -= 1;
        month = 12;
    }
    return [NSString stringWithFormat:@"%d-%@",year,[NSString format:month]];
}
+ (NSString *)getCurrentDate
{
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
    [dateformate setDateFormat:@"yyyyMMdd"];
    return [dateformate stringFromDate:localeDate];
}

//+(NSString *)getCurrent13String:(NSString *)timeInterval
//{
//    if ([NSString isBlankString:timeInterval]) {
//        return @"";
//    }
//    return [NSDate getTimeInterval:[timeInterval doubleValue] / 1000];
//}
//+(NSString *)getCurrentString:(NSString *)timeInterval
//{
//    if ([NSString isBlankString:timeInterval]) {
//        return @"";
//    }
//    return [NSDate getTimeInterval:[timeInterval longLongValue]];
//}


+(NSString *)formatTimeSinceNow:(NSTimeInterval)timestamp{
    NSLog(@"timestamp:%lld",(long long)timestamp);
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
    NSString *confromTimespStr = [dateFormatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

+ (NSString *)getTimeInterval:(NSTimeInterval )intervalTime
{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:intervalTime];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
    NSString *confromTimespStr = [dateFormatter stringFromDate:confromTimesp];
    return confromTimespStr;
    
//    NSDate *date = (intervalTime > 0) ? [NSDate dateWithTimeIntervalSince1970:intervalTime] : nil;
//    NSCalendar *calender = [NSCalendar currentCalendar];
//    NSDateComponents *components = [calender components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date toDate:[NSDate date] options:0];
//    
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    NSString *showTime = nil;
//    NSInteger year = [components year];
//    NSInteger month = [components month];
//    NSInteger day = [components day];
//    //    NSInteger hour = [components hour];
//    //    NSInteger min = [components minute];
//    //    NSInteger sec = [components second];
//    if (year > 0){
//        formatter.dateFormat = @"MM月dd日HH时mm分";
//        showTime = [formatter stringFromDate:date];
//    }else {
//        if (month>0|| (month == 0 && day >7)){
//            formatter.dateFormat = @"MM月dd日HH时mm分";
//            showTime = [formatter stringFromDate:date];
//        }
//        else {
//            if (day <= 7 && day >1){
//                formatter.dateFormat = @"MM月dd日HH时mm分";
//                showTime = [formatter stringFromDate:date];
//            }
//            else if(1 == day)
//            {
//                formatter.dateFormat = @"HH时mm分";
//                showTime = [formatter stringFromDate:date];
//                showTime = [NSString stringWithFormat:@"昨天%@", showTime];
//            }else {
//                formatter.dateFormat = @"HH时mm分";
//                showTime = [formatter stringFromDate:date];
//            }
//        }
//    }
//    return showTime;
}

@end
